// cart.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address: '广东省广州市天河区体育中心28号天河城广场北座 1号楼608',
    time:'收货时间不限',
    name:'张三',
    phone:'15011112222',
    showMsk: false,
    showdetail: false,
    check:[],
    total: 0
  
  },
  check(e){
    let index = e.currentTarget.dataset.index
    let check = this.data.check
    check[index].status = !check[index].status
    this.setData({
      check: check
    })
    this.computed_total()
    
  },
  computed(e){
    let type_ = e.currentTarget.dataset.type
    let num = e.currentTarget.dataset.number
    let index = e.currentTarget.dataset.index
    let check = e.currentTarget.dataset.check
    let id = e.currentTarget.dataset.id 
    // console.log(type_, num, index, check)
    let data = this.data.data
    let stock = data.data[index].stock
    // console.log(data)
    if(type_ == 'down' && num > 1){
      data.data[index].num --
    } else if (type_ == 'up' && num <= stock){
      data.data[index].num ++
    }
    this.changeNum(id, data.data[index].num)
    console.log(data[index])
    this.setData({
      data
    })
    this.computed_total()
  },
  computed_total(){
    let total = 0
    this.data.check.forEach( (value,index) => {
      if(value.status){
        total += parseFloat(this.data.data.data[index].price) * this.data.data.data[index].num 
      }
    })
    total = total.toFixed(2);
    this.setData({ 
      total
    })
  },
  del(e){
    let index = e.currentTarget.dataset.index
    let id = e.currentTarget.dataset.id
    let goods = this.data.goods
    let check = this.data.check
    let self = this
    wx.showModal({
      title: '提示',
      content: '确认删除吗？',
      success: function (res) {
        if (res.confirm) {
          self.del_method(id, () => {
            self.getData()
          })
        }
      }
    })
  },
  del_method(id,callback){
    wx.showLoading({
      title: '正在删除...',
      mask: true
    })
    let baseUrl = getApp().globalData.baseUrl,
      access_token = getApp().globalData.access_token,
      header = getApp().globalData.header,
      self = this
    wx.request({
      url: baseUrl + '/Car/delete_car',
      method: 'post',
      data: { access_token, id },
      header,
      success: data => {
        wx.hideLoading()
        if(data.data.code == 200){
            wx.showToast({
              title: '删除成功！',
            })
            callback()
        }
      }
    })
  },
  hide_msk(e){
    // console.log(e.target.dataset.name)
    if(e.target.dataset.name == 'msk'){
      this.setData({
        showMsk: false
      })
    }
  },
  // 提交订单
  submit(){
    let self = this,
      check = '',
      car_id = '',
      num = '',
      baseUrl = getApp().globalData.baseUrl,
      access_token = getApp().globalData.access_token,
      header = getApp().globalData.header
    this.data.check.forEach( (value,index) => {
      car_id += this.data.data.data[index].id + ','
      if (value.status){
        check += this.data.data.data[index].id + ','
      }
      num += this.data.data.data[index].num + ','
    })
    check =  check.substr(0, check.length-1)
    car_id = car_id.substr(0, car_id.length - 1)
    num = num.substr(0, num.length-1)
    if(!check){
      wx.showToast({
        title: "至少要选择一个商品项",
      })
    }else{
      wx.navigateTo({
        url: `../confirm-order/confirm-order?isBuyNow=false&&check=${check}&&car_id=${car_id}&&num=${num}`,
        success: function(res) {
          console.log('购物车结算',res.data)
        },
        fail: function(res) {},
        complete: function(res) {},
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let self = this
    if (!getApp().globalData.access_token){
      getApp().setUser(function(){
        return self.getData()
      })
    }else{
      self.getData()
    }
  },
  showdetail(){
    this.setData({
      showdetail: !this.data.showdetail
    })
  },
  getData(){
    wx.showLoading({
      title: '正在加载...',
      mask: true
    })
    let baseUrl = getApp().globalData.baseUrl,
        access_token = getApp().globalData.access_token,
        self = this
    wx.request({
      url: baseUrl + '/Car/index',
      data: { access_token  },
      method: 'get',
      success: data => {
        wx.hideLoading()
        console.log(data.data)
        if(data.data.code == 200){
          self.setData({
            data: data.data.data
          })
          self.initData()
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },
  // 初始化数据
  initData(){
    let self = this
    let data = []
    let touchup = [],
      touchdown = []
    self.data.data.data.forEach((value, index) => {
      value.num = parseInt(value.num)
      data.push({
        status: false,
        price: ''
      })
    })
    self.setData({
      check: data
    })
  },
  // 修改数量
  changeNum(product_id,num){
    wx.showLoading({
      title: '正在加载...',
      mask: true
    })
    let baseUrl = getApp().globalData.baseUrl,
      access_token = getApp().globalData.access_token,
      header = getApp().globalData.header,
      self = this
      wx.request({
        url: baseUrl + '/Car/setNum',
        method: 'post',
        header,
        data: { access_token, product_id, num},
        success: data => {
          wx.hideLoading()
          if(data.data.code==400){
            wx.showToast({
              title: data.data.message,
            })
          }
        }
      })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})